package com.drug.controller;

import com.drug.domain.DrugAtegory;
import com.drug.domain.DrugInformation;
import com.drug.domain.Result;
import com.drug.service.Supplier2Service;
import com.drug.util.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * 供货商管理的控制层
 */
@RestController
@RequestMapping("/suppliser")
public class Supplier2Controller {

    @Autowired
    private Supplier2Service supplier2Service;
    /**
     * 查询所有的药品类别
     * @return
     */
    @RequestMapping("/findAllDC")
    private Result findAllDC(){
        return  supplier2Service.findAllDC();
    }
    /**
     * 查询所有的药品类别
     * @return
     */
    @RequestMapping("/findAllSp")
    private Result findAllSp(){
        return  supplier2Service.findAllSp();
    }

    /**
     * 查询所有的剂型
     * @return
     */
    @RequestMapping("/findAllDF")
    private Result findAllDF(){
        return  supplier2Service.findAllDF();
    }

    /**
     *
     * 根据条件分页查询所有或多条件查询
     * @param map
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/findConditional")
    private Result findConditional(@RequestParam Map<String,Object> map,@RequestParam(required=true,defaultValue="1")Integer pageNum){
      return  supplier2Service.findConditional(map,pageNum);
    }


    /**
     * 查询所有的质量层次
     * @return
     */
    @RequestMapping("/findAllDQL")
    private Result findAllDQL(){
        return  supplier2Service.findAllDQL();
    }

    /**
     *
     * 根据条件分页查询所有或多条件查询
     * @param map
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/findDI")
    private Result findDI(@RequestParam Map<String,Object> map,@RequestParam(required=true,defaultValue="1")Integer pageNum){
        return  supplier2Service.findDI(map,pageNum);
    }

    /**
     *
     * 根据条件分页查询所有或多条件查询
     * @param map
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/findDIs")
    private Result findDIs(@RequestParam Map<String,Object> map,@RequestParam(required=true,defaultValue="1")Integer pageNum){
        return  supplier2Service.findDIs(map,pageNum);
    }

    /**
     * 药品品目导出
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/selectDa")
    public String export(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String message = "";
        //获取数据
        List<DrugInformation> list = supplier2Service.findAll();
        //excel标题
        String[] title = {"药品目录号","通用名","剂型","规格","单位","转换系数","药品类别","状态"};
        //excel文件名
        String fileName = "drugDa"+System.currentTimeMillis()+".xls";
        //sheet名
        String sheetName = "样品信息表";
        String [][] content = new String[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            DrugInformation drugInformation = list.get(i);
            DrugAtegory drugAtegory = drugInformation.getDrugAtegory();
            Integer daNumber = null;
            if (drugAtegory!=null){
                daNumber = drugAtegory.getDaNumber();
            }
            content[i][0] = daNumber+"";
            //通用名
            String diName = drugInformation.getDiName();
            content[i][1] = diName+"";
            //剂型
            String diForm = drugInformation.getDiForm();
            content[i][2] = diForm+"";
            //规格
            String diSpecifications = drugInformation.getDiSpecifications();
            content[i][3] = diSpecifications+"";
            //单位
            content[i][4] = drugInformation.getDiUnit()+"";
            //转换系数
            content[i][5] = drugInformation.getDiCc()+"";
            //类别
            Integer diDaCategory = drugInformation.getDiDaCategory();
            String nameById =null;
            if (diDaCategory!=null){
                nameById  = supplier2Service.findNameById(diDaCategory);
            }
            content[i][6] = nameById +"";
            //状态
            String diState="";
            diState = drugInformation.getDiState();
            if (diState.equals("0")){
                diState="正常";
            }else {
                diState="异常";
            }
            content[i][7] = diState +"";
        }
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
            message = "SUCCESS";
        } catch (Exception e) {
            e.printStackTrace();
            message = "ERROR";
        }
        return null;
    }
    /**
     * 药品信息导出
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/selectDi")
    public String exportDi(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String message = "";
        //获取数据
        List<DrugInformation> list = supplier2Service.findAll();
        //excel标题
        String[] title = {"药品流水号","通用名","剂型","规格","单位","转换系数","生产企业","商品名称","中标价","质量层次","药品类别","交易状态"};
        //excel文件名
        String fileName = "drugDi"+System.currentTimeMillis()+".xls";
        //sheet名
        String sheetName = "样品信息表";
        String [][] content = new String[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            DrugInformation drugInformation = list.get(i);
            //药品流水号
            String diNumer = drugInformation.getDiNumer();
            content[i][0] = diNumer+"";
            //通用名
            String diName = drugInformation.getDiName();
            content[i][1] = diName+"";
            //剂型
            String diForm = drugInformation.getDiForm();
            content[i][2] = diForm+"";
            //规格
            String diSpecifications = drugInformation.getDiSpecifications();
            content[i][3] = diSpecifications+"";
            //单位
            content[i][4] = drugInformation.getDiUnit()+"";
            //转换系数
            content[i][5] = drugInformation.getDiCc()+"";
            //生产企业
            String mfNameById = null;
            String daName= null;
            DrugAtegory drugAtegory = drugInformation.getDrugAtegory();
            if (drugAtegory!=null){
               String daMName = drugAtegory.getDaMName();
                 if (daMName!=null){
                  mfNameById  = supplier2Service.findMfNameById(Integer.parseInt(daMName));
                 }
                 daName = drugAtegory.getDaName();
            }
            content[i][6] =mfNameById+"";
            //商品名称
            content[i][7] = daName +"";
            //中标价
            content[i][8] = drugInformation.getDiNewPrice() +"";
            //质量层次
            String dqlNameById= null;
            String diDqlName = drugInformation.getDiDqlName();
            if (diDqlName!=null){
                 dqlNameById = supplier2Service.findDqlNameById(Integer.parseInt(diDqlName));
            }
            content[i][9] =dqlNameById+"";
            //类别
            Integer diDaCategory = drugInformation.getDiDaCategory();
            String nameById =null;
            if (diDaCategory!=null){
                nameById  = supplier2Service.findNameById(diDaCategory);
            }
            content[i][10] = nameById +"";
            //交易状态
            String diTradingStatu="";
            String diTradingStatus = drugInformation.getDiTradingStatus();
            if (diTradingStatus!=null){
                if (diTradingStatus.equals("0")){
                    diTradingStatu="正常";
                }else {
                    diTradingStatu="异常";
                }
            }
            content[i][11] = diTradingStatu +"";
        }
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
            message = "SUCCESS";
        } catch (Exception e) {
            e.printStackTrace();
            message = "ERROR";
        }
        return null;
    }
    private void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"utf-8");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 根据daid查询相依的目录详细信息
     * @param daId
     * @return
     */
    @RequestMapping("/findDaOne")
    private Result findDaOne(@RequestParam(value = "daId", required = false) Integer daId){
       return supplier2Service.findDaOne(daId);
    }


    /**
     * 根据daid查询相依的目录详细信息
     * @param diId
     * @return
     */
    @RequestMapping("/findDiOne")
    private Result findDiOne( Integer diId){
        return supplier2Service.findDiOne(diId);
    }



    /**
     * 查询所有的供应商
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectAll", produces = "application/json; charset=utf-8")
    public Result fandAll(){
        return supplier2Service.selectAll();
    }
}
